﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using URIDecoder;

namespace ConfigWindows
{
    public partial class Form1 : Form
    {
        ConfigFile Config { get; set; }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Config = ConfigFile.FromFile();
            foreach (var item in Config.Schemes)
            {
                SchemeControl sc = new SchemeControl(item);
                sc.Deleted += new EventHandler(sc_Deleted);
                flowLayoutPanel1.Controls.Add(sc);
            }
            foreach (var item in Config.Downloaders)
            {
                DownloaderControl dc = new DownloaderControl(item);
                dc.BecomeDefault += new EventHandler(dc_BecomeDefault);
                if (Config.DefaultDownloader == item.Name) dc.SetDefaultDownloader(true);
                flowLayoutPanel2.Controls.Add(dc);
            }
            if (string.IsNullOrEmpty(Config.DefaultDownloader))
                DefaultDownloader.Checked = true;

        }
        void dc_BecomeDefault(object sender, EventArgs e)
        {
            DownloaderControl dc = (DownloaderControl)sender;
            Config.DefaultDownloader = dc.Downloader.Name;
            foreach (var item in flowLayoutPanel2.Controls)
            {
                DownloaderControl ddc = item as DownloaderControl;
                if (ddc != null && ddc != dc)
                    ddc.SetDefaultDownloader(false);
            }
            DefaultDownloader.Checked = false;
        }

        void sc_Deleted(object sender, EventArgs e)
        {
            Config.Schemes.Remove(((SchemeControl)sender).Scheme);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (SchemeControl item in this.flowLayoutPanel1.Controls)
                item.Save();
            foreach (var item in this.flowLayoutPanel2.Controls)
            {
                DownloaderControl dc = item as DownloaderControl;
                if (dc != null) dc.Save();
            }
            Config.ToFile();
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (SchemeControl item in this.flowLayoutPanel1.Controls)
                item.SetHandler(sender, e);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            EncodedURIScheme scheme =
                new EncodedURIScheme
                {
                    Scheme = "newscheme",
                    EncodedUriRegex = "^(.*)$",
                    OriginalUriRegex = "^(.*)$",
                    EncodeMethod = "plaintext",
                    Name = "新协议",
                };
            Config.Schemes.Add(scheme);
            SchemeControl sc = new SchemeControl(scheme);
            sc.Deleted += new EventHandler(sc_Deleted);
            flowLayoutPanel1.Controls.Add(sc);
            flowLayoutPanel1.ScrollControlIntoView(sc);
        }

        private void DefaultDownloader_CheckedChanged(object sender, EventArgs e)
        {
            if (DefaultDownloader.Checked)
            {
                Config.DefaultDownloader = string.Empty;
                foreach (var item in this.flowLayoutPanel2.Controls)
                {
                    DownloaderControl dc = item as DownloaderControl;
                    if (dc != null) dc.SetDefaultDownloader(false);
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            DefaultDownloader.Checked = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Downloader dl =
                new Downloader
                {
                    Name = "新下载程序",
                    FileName = "<请填写程序路径>",
                    ArgumentsFormat = "{0}",
                };
            Config.Downloaders.Add(dl);
            DownloaderControl dc = new DownloaderControl(dl);
            dc.BecomeDefault += new EventHandler(dc_BecomeDefault);
            flowLayoutPanel2.Controls.Add(dc);
            flowLayoutPanel2.ScrollControlIntoView(dc);
        }
    }
}
